package com.example.zhilongzhang.constructionpayapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.zhilongzhang.constructionpayapp.R;
import com.example.zhilongzhang.constructionpayapp.adapter.RecordAdapter;
import com.example.zhilongzhang.constructionpayapp.base.BaseActivity;
import com.example.zhilongzhang.constructionpayapp.bean.RecordListBean;
import com.example.zhilongzhang.constructionpayapp.http.HttpMethod;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

public class RecordActivity extends BaseActivity {
    RecyclerView mRecyclerView;
    List<RecordListBean.DataEntity> list = new ArrayList<>();
    private RecordAdapter adapter;
    RefreshLayout refreshLayout;
    int page = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        mRecyclerView = findViewById(R.id.listview);
        refreshLayout = findViewById(R.id.refreshLayout);
        initData();
        setListView();
    }

    private void initData() {
        findViewById(R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setListView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RecordAdapter(R.layout.item_record, list);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(RecordActivity.this, BillActivity.class);
                intent.putExtra("item",list.get(position));
                startActivity(intent);
            }
        });
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadData();
            }
        });
    }

    private void getData() {
        page = 1;
        HttpMethod.getdatalist("14", page, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                refreshLayout.finishRefresh();
            }

            @Override
            public void onResponse(String response) {
                list.clear();
                RecordListBean bean = new Gson().fromJson(response, RecordListBean.class);
                if (bean.getCode()!=0){
                    return;
                }
                list.addAll(bean.getData());
                adapter.notifyDataSetChanged();
                refreshLayout.finishRefresh();
            }
        });
    }

    private void LoadData() {
        page = page + 1;
        HttpMethod.getdatalist("14", page, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onResponse(String response) {
                RecordListBean bean = new Gson().fromJson(response, RecordListBean.class);
                if (bean.getCode()!=0){
                    return;
                }
                list.addAll(bean.getData());
                adapter.notifyDataSetChanged();
                refreshLayout.finishLoadMore();
            }
        });
    }
}
